﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace InhollandAir.App_Code
{
    public class Aircraft
    {
        private int mId;
        private int mSeats;

        public void LoadFromDb(int aircraftId)
        {
            //get flight
            var connUser = new SqlConnection(WebConfigurationManager.ConnectionStrings["FlightReservation"].ToString());

            if (connUser.State == ConnectionState.Closed) connUser.Open();

            try
            {
                string sqlText = "SELECT * FROM aircraft WHERE aircraftID = @aircraftId";
                SqlCommand cmd = new SqlCommand(sqlText, connUser);
                cmd.Prepare();

                cmd.Parameters.AddWithValue("@aircraftId", aircraftId);

                SqlDataReader myReader = cmd.ExecuteReader();
                if (myReader.Read())
                {
                    this.mId = (int)myReader["aircraftID"];
                    this.mSeats = (int)myReader["seats"];
                }
                else
                {
                    throw new Exception("No aircraft found with aircraftID= " + id);
                }
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connUser.Close();
            }  
        }

        #region Properties

        public int id
        {
            get { return mId; }
            set { mId = value; }
        }

        public int seats
        {
            get { return mSeats; }
            set { mSeats = value; }
        }
        #endregion
    }
}